package com.example.demodatadruid.dao;


import com.example.demodatadruid.Entity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DepartmentDao {
    @Autowired(required = false)
    JdbcTemplate jdbcTemplate;

    public List<Department> getAllDepartments(){
        String sql = "select * from department";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Department.class));

    }
    public Department getDepartmentById(Integer id) {
        String sql = "select * from department where id=?";
        RowMapper<Department> rowMapper=new BeanPropertyRowMapper<>(Department.class);
        return jdbcTemplate.queryForObject(sql,rowMapper,id);

    }
    public Integer addDepartment(Department department){
        String sql = "insert into department(id,department_name)" +
                "values(?,?)";
        return jdbcTemplate.update(sql, department.getId(),department.getDepartment_name());

    }
    public Integer updateDepartment(Department department){
        String sql = "update department set department_name=? where id=?";
        return jdbcTemplate.update(sql,department.getDepartment_name(),department.getId());
    }
    public Integer deleteDepartmentById(Integer id){
        String sql = "delete from department where id =?";
        return jdbcTemplate.update(sql, id);
    }
}
